// pages/needList/index.js
import axios from '../network/index'
let pageNum = 1
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    showTime: false,
    searchVal: "",
    noMore: false,
    value1: 0,
    searchDate: '',
    currentDate: new Date().getTime(),
    searchStatus: "",
    titleName: '选择状态',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      } else if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    option1: [{
      text: '全部',
      value: 0
    }, ],
    option3: [{
        text: '待采购',
        value: 'waitPurchase'
      },
      {
        text: '已确认',
        value: 'confirmedPurchase'
      },
      {
        text: '已发货',
        value: 'waitDeliver'
      },
      {
        text: '待验收',
        value: 'waitAccept'
      },
      {
        text: '完成',
        value: 'complete'
      },
    ],
  },
  add0(m) {
    return m < 10 ? '0' + m : m
  },
  //菜单项变化事件
  selectChange(e) {
    let index = e.currentTarget.dataset.index
    switch (index) {
      case '0':
        break;
      case '1':
        break;
      case '2':
        this.setData({
          searchStatus: e.detail,
          titleName: this.data.option3.find(item => item.value === e.detail).text
        })
        pageNum = 1
        this.getNeedList(pageNum)
        break;
    }
  },
  //跳转详情
  toDetail(e) {
    let orderNo = e.currentTarget.dataset.orderno
    wx.navigateTo({
      url: '/pages/checkAndAccept/index?orderNo=' + orderNo,
    })
  },
  searchIpt(e) {
    this.setData({
      searchVal: e.detail
    })
    pageNum = 1
    this.getNeedList(pageNum)
  },
  //获取列表数据
  getNeedList(pageNum, more) {
    axios({
      url: "/demandApply/list",
      method: 'post',
      data: {
        pageNum,
        pageSize: 10,
        searchVal: this.data.searchVal,
        searchStatus: this.data.searchStatus,
        searchDate: this.data.searchDate,
        userId: wx.getStorageSync('userId')
      }
    }).then(res => {
      if (res.code == 200) {
        if (more) {
          if (res.data.content.length < 10) {
            this.setData({
              noMore: true
            })
          }
          this.setData({
            list: this.data.list.concat(res.data.content)
          })
        } else {
          this.setData({
            list: res.data.content
          })
        }

      }
    })
  },
  //打开时间选择器
  openSelectTime() {
    this.setData({
      showTime: true
    })
  },
  offSelectTime() {
    this.setData({
      showTime: false
    })
    this.selectComponent('#clickTime').toggle()
  },
  //确认时间
  confirmTime(e) {
    this.setData({
      currentDate: e.detail,
      searchDate: this.format(e.detail),
      showTime: false
    })
    this.selectComponent('#clickTime').toggle()
    pageNum = 1
    this.getNeedList(pageNum)
  },
  format(e) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(e);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return y + '-' + this.add0(m) + '-' + this.add0(d)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNeedList(pageNum)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.noMore) {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none"
      })
      return
    }
    pageNum++
    this.getNeedList(pageNum, true)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})